package com.owlsmart.bluecat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.owlsmart.bluecat.R;
import com.owlsmart.bluecat.entity.Subjects;
import com.owlsmart.bluecat.listener.OnItemChildClickListener;
import com.owlsmart.bluecat.listener.OnItemClickListener;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.List;

/**
 * @author mingtai
 * @description 写一个适配器，用来展示数据,每一个fragment都要有一个适配器
 * @time 2020/8/11 0:21
 */

public class MovieListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //上下文
    private Context mContext;
    //传过来的对象
    private List<Subjects> mData;

    public void setDatas(List<Subjects> mData) {
        this.mData = mData; //不用构造器，可以不用反复创建对象
    }

    public MovieListAdapter(Context context) {
        this.mContext = context;
    }

    public MovieListAdapter(Context context, List<Subjects> mData) {
        this.mContext = context;
        this.mData = mData;
    }

    private OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载布局文件
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_hotmovie_layout, parent, false);
        HotMovieViewHolder hotMovieViewHolder = new HotMovieViewHolder(view);
        return hotMovieViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        // 添加不同的ViewHolderThree 绑定不同的数据
        HotMovieViewHolder vh = (HotMovieViewHolder) holder;
        //    绑定数据，给控件对象赋值。
        Subjects subjects = mData.get(position);

        vh.title.setText(subjects.getTitle());
        // vh.imageView.setText(newsEntity.getAuthorName());

        // 异步加载图片
        // Picasso.with(mContext)
        //         .load(newsEntity.getHeaderUrl())
        //         .transform(new CircleTransform()) // 绘制圆形头像
        //         .into(vh.header);
        //mThumb 缩略图

        Picasso.with(mContext).load(subjects.getCover())
                .into(vh.imageView);
        vh.mSubjects = subjects;

    }


    @Override
    public int getItemCount() {
        // recycleView 列表里面有多少项
        if (mData != null && mData.size() > 0) {
            return mData.size();
        }
        return 0;
    }

    // 写一个类，获取view对象中的每一条数据,然后在其他地方赋值。适配器的作用？
    public class HotMovieViewHolder extends RecyclerView.ViewHolder  {
        //获取第一个布局的控件，然后在其他地方进行赋值
        private TextView title;
        private ImageView imageView;
        private Subjects mSubjects;

        public HotMovieViewHolder(@NonNull View view) {
            super(view);
            // 获得第一个布局所有控件对象
            title = view.findViewById(R.id.hot_movie_title);
            imageView = view.findViewById(R.id.hot_movie_img);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(mSubjects);
                }
            });
            //通过tag将ViewHolder和itemView绑定
            view.setTag(this);
        }


    }

    public interface OnItemClickListener {
        void onItemClick(Subjects mSubjects);
    }

}
